#This document was created by R Markdown in R
#This document contains interactive maps

R and Rstudio

Developed at the University of Auckland, New Zealand, in 1993, R is a specialized programming language for statistics and data analysis. Because R is based on open-source project, it is not only free of charge and but also allowed to be modified and redistributed. In particular, social scientists have started to focus on R’s outstanding extensibility since 2005, which has led to R’s remarkable growth among statistical programs. Packages are one of the powerful features of R, with 15,503 currently registered (March 25, 2020) and added by users in real-time.

Rstudio is an Integrated Development Environment (IDE) software of R. It provides a programming environment where coding, debugging, compiling, and distributing can be addressed in one program. Moreover, Rstudio provides users an interface easier to use than R.

R(Left) and Rstudio(Right)

Packages for Mapping and Data Visualization

R’s packages support producing various types of maps and graphs. For example, (1) a simple vector-based map can be created by map package. (2) new spatial information can be added on Google Map with the combination of ggplot and ggmap packages. (3) a spatial analysis package, sp package, allows users to use R as Geographic Information System. (4) R has been supporting to create an interactive web map since leaflet package was first launched in June.

library(htmltools); library(leaflet)
isu <- data.frame(Name=c("College of Design", "Memorial Union", "Parks Library"), Lat=c(42.0285, 42.0235, 42.0281), Long=c(-93.6532, -93.6458, -93.6487))
m1 <- leaflet(isu) %>% addTiles() %>% addMarkers(~Long, ~Lat, popup = ~htmlEscape(Name)); m1

Web Mapping Project

Because I have never created web mapping with the R program before, I tried coding the CRP 558 Assignment 2 by using R’s leaflet package. The topic is as follows:
The State of Utah is the earthquake-vulnerable region because it is located on the Wasatch Fault. It means that earthquakes with a magnitude of 4.0 or higher, which can cause physical damage, are likely to occur. In particular, when occurring earthquakes, it is expected that areas with a high population density will have severe damage. Therefore, based on the location of past earthquakes between 2000 and 2010, this project aimed to analyze where is the earthquake-vulnerable area in Utah. View the Assignment2

The R code script was written in the following steps:

library(leaflet); library(rgdal); library(geojsonio); library(htmltools)
  1. The four packages were loaded in R for creating the interactive map: leaflet package, rgdal package, geojsonio package, and htmltools package.
eq <- geojsonio::geojson_read("eq.geojson", what = "sp")
popden <- geojsonio::geojson_read("pop_den.geojson", what = "sp")

pal <- colorNumeric("magma", NULL)
  1. The geoJSON files were loaded and assigned to each variable name, eq, and popden. The pal variable is for selecting a choropleth map’s color.
leaflet(popden) %>% addTiles(group = "OSM (Default Map)") %>%
  addProviderTiles(providers$CartoDB.Positron, group = "Cartographic Map") %>% 
  addProviderTiles(providers$Esri.NatGeoWorldMap, group = "National Geographic Map") %>% 
  addPolygons(weight = 0.5, color = "black", smoothFactor = 0.3, fillOpacity = 0.7, fillColor = ~pal(Density), group = "Population Density", label = ~paste0(County, " COUNTY: ", formatC(Density, big.mark = ","),
" per sq. mi.")) %>%
  addLegend("bottomright", colors =c("#303033",  "#3a384f", "#5b3c84", "#784193", "#8b4894", "#f9fac7"), labels=c("1.0-8.0", "8.0-19.5","19.5-96.0","96.0-350.6","350.6-483.6", "483.6-1274.7"), title= "POP Density 2015",
opacity = 1, group = "Population Density") %>% addTiles() %>%
  addMarkers(lng = eq$longitude, lat = eq$latitude, label = ~as.character(eq$mag), group = "Earthquakes") %>%
  addLayersControl(baseGroups = c("OSM (Default Map)", "Cartographic Map", "National Geographic Map"), overlayGroups = c("Earthquakes", "Population Density"), options = layersControlOptions(collapsed = FALSE))

3. The functions below were applied to making my interactive map. The brief explanation is as follows:
(1) leaflet(): Loading a variable into leaflet function
(2) addTiles(): Adding Open Street Map into a leaflet map
(3) addProviderTiles(): Adding various leaflet basemaps into a leaflet map
(4) addPolygons(): Creating a polygon layer from geoJSON file and adding it into a map
(5) addLegend(): Creating a legend for a layer
(6) addMarkers(): Creating a markers layer into a map
(7) addLayersControl(): Adding a layer control box in a map

Overall Evaluation

R language is not an easy programming language for first-time users. The difficulty results from the features of the R language that enables programming as well as having various data analysis functions. Although the R language’s syntax composition seems complex compared to SPSS or STATA, the coding style language not only helps R have the highest degree of freedom among statistic programs but also helps users share their code scripts. Because of these characteristics, R has a lot of user-centered communities and users can share numerous R-related information in there.
- R-bloggers (https://www.r-bloggers.com)
- Cross Validated (https://stats.stackexchange.com)
This project was also carried out with shared information from numerous R users. Even though I have never used R’s leaflet package before, the vast leaflet coding information in Google allowed me to write this code script to achieve my project goal. This experience let me believe the infinite possibilities of R.

Resources or Dataset

eq.geojson
- Data Explanation: Earthquakes Information in Utah
- GitHub URL: https://andyjung82.github.io/CRP558/assignment2_geojson/utah_earthquake4.geojson
- Source Data URL: https://earthquake.usgs.gov/earthquakes/search/

pop_den.geojson
- Data Explanation: Population Density Information in 29 counties, Utah
- GitHub URL: https://andyjung82.github.io/CRP558/assignment2_geojson/pop_den.geojson
- Source Data URL: https://opendata.utah.gov/Government-and-Taxes/Population-Density-By-Land-Area-And-County-In-Utah/bzur-buif

Reference
- Brunsdon, C., & Comber, L. (2015). An introduction to R for spatial analysis and mapping. Sage.